using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Drawing;

using SlimDX;
using SlimDX.Direct3D9;

using miciv.Misc;

namespace miciv.Rendering
{
    public class RenderContextClearOptions : IRenderContextClearOptions
    {
        protected Device m_d3dDevice;
        protected bool m_bEnabled;
        protected ClearFlags m_flags;
        protected Color4 m_f4Color;
        protected float m_depth;
        protected int m_stencil;
        protected Rectangle[] m_rectangles;

        public RenderContextClearOptions(CoreContext _coreContext)
        {
            this.Device = _coreContext.GetData<SlimDX.Direct3D9.Device>("D3DDevice");
            this.Enabled = false;
            this.Flags = ClearFlags.Target | ClearFlags.ZBuffer;
            this.Color = new Color4(System.Drawing.Color.DarkGray);
            this.Depth = 1.0f;
            this.Stencil = 0;
            this.Rectangles = null;
        }

        #region IRenderContextClearOptions Membres

        public Device Device
        {
            get
            {
                return this.m_d3dDevice;
            }
            set
            {
                this.m_d3dDevice = value;
            }
        }

        public bool Enabled
        {
            get
            {
                return this.m_bEnabled;
            }
            set
            {
                this.m_bEnabled = value;
            }
        }

        public ClearFlags Flags
        {
            get
            {
                return this.m_flags;
            }
            set
            {
                this.m_flags = value;
            }
        }

        public SlimDX.Color4 Color
        {
            get
            {
                return this.m_f4Color;
            }
            set
            {
                this.m_f4Color = value;
            }
        }

        public float Depth
        {
            get
            {
                return this.m_depth;
            }
            set
            {
                this.m_depth = value;
            }
        }

        public int Stencil
        {
            get
            {
                return this.m_stencil;
            }
            set
            {
                this.m_stencil = value;
            }
        }

        public Rectangle[] Rectangles
        {
            get
            {
                return this.m_rectangles;
            }
            set
            {
                this.m_rectangles = value;
            }
        }

        public void Clear()
        {
            if (false != this.Enabled)
            {
                this.Device.Clear(this.Flags, this.Color, this.Depth, this.Stencil, this.Rectangles);
            }
        }

        #endregion
    }

    public class RendererContext : IRendererContext
    {
        protected string m_strID;
        public string ID
        {
            get
            {
                return this.m_strID;
            }
            set
            {
                this.m_strID = value;
            }
        }

        protected ICamera m_camera;
        public ICamera Camera
        {
            get
            {
                return this.m_camera;
            }
            set
            {
                this.m_camera = value;
            }
        }

        protected Viewport m_viewport;
        public Viewport Viewport
        {
            get
            {
                return this.m_viewport;
            }
            set
            {
                if (value != this.m_viewport)
                {
                    this.m_viewport = value;
                    this.m_bIsNullViewport = (0 == this.m_viewport.Height) || (0 == this.m_viewport.Width);
                }
            }
        }

        protected bool m_bIsNullViewport;
        public bool IsNullViewport
        {
            get
            {
                return this.m_bIsNullViewport;
            }
        }

        protected ScreenSizeChangedHandler m_ScreenSizeChanged;
        public ScreenSizeChangedHandler ScreenSizeChanged
        {
            get
            {
                return this.m_ScreenSizeChanged;
            }
            set
            {
                this.m_ScreenSizeChanged = value;
            }
        }

        protected IRenderContextClearOptions m_clearOptions;
        public IRenderContextClearOptions ClearOptions
        {
            get
            {
                return this.m_clearOptions;
            }
            set
            {
                this.m_clearOptions = value;
            }
        }

        protected UserHandler m_BeginScene;
        public UserHandler BeginScene
        {
            get
            {
                return this.m_BeginScene;
            }
            set
            {
                this.m_BeginScene = value;
            }
        }

        protected UserHandler m_EndScene;
        public UserHandler EndScene
        {
            get
            {
                return this.m_EndScene;
            }
            set
            {
                this.m_EndScene = value;
            }
        }

        public RendererContext(CoreContext _coreContext)
        {
            this.m_bIsNullViewport = true;
            this.ClearOptions = new RenderContextClearOptions(_coreContext);
        }

        public void Clear()
        {
            if (null != this.ClearOptions)
            {
                this.ClearOptions.Clear();
            }
        }
    }
}
